<template>
    <div class="login-container">
        <el-form :model="loginForm" :rules="rules" ref="loginForm" class="login-form">
            <h3 class="title">车辆投诉系统登录</h3>
            <el-form-item prop="account">
                <el-input v-model="loginForm.account" type="text" placeholder="用户名">
                    <template slot="prepend">
                        <i class="el-icon-user"></i>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginForm.password" type="password" placeholder="密码">
                    <template slot="prepend">
                        <i class="el-icon-lock"></i>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" style="width:100%;" @click="handleLogin">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import md5 from 'md5';

export default {
    name: 'Login',
    data() {
        return {
            loginForm: {
                account: '',
                password: ''
            },
            rules: {
                account: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
            }
        }
    },
    methods: {
        async handleLogin() {
            try {
                await this.$refs.loginForm.validate();
                const response = await this.$axios({
                    method: 'post',
                    url: '/car/login',
                    data: {
                        account: this.loginForm.account,
                        password: md5(this.loginForm.password)
                    }
                });
                
                if (response.data.code === 0) {
                    localStorage.setItem('token', response.data.data.token);
                    this.$message.success('登录成功');
                    await this.$router.replace('/complaint/list');
                } else {
                    this.$message.error(response.data.msg || '登录失败')
                }
            } catch (error) {
                console.error('登录失败:', error);
                this.$message.error('登录失败，请重试')
            } finally {
                this.loading = false;
            }
        }
    }
}
</script>

<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f3f3f3;
}

.login-form {
    width: 350px;
    padding: 35px;
    border-radius: 5px;
    background: #fff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
}

.title {
    text-align: center;
    margin: 0 0 30px;
    color: #303133;
}
</style>